package com.example.widgetcase.slice;

import com.example.widget_lib.bean.CircleBean;
import com.example.widget_lib.widget.legend.LegendView;
import com.example.widget_lib.widget.ring.RingView;
import com.example.widgetcase.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class LegendAbilitySlice extends BaseSlice {

    private DecimalFormat percentFormat = new DecimalFormat("#.##%");
    private RingView ringv2;
    private LegendView mLegendView;
    private List<String> mLabels;

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_legend_view;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mLabels = new ArrayList<>();
        mLegendView = (LegendView) findComponentById(ResourceTable.Id_legendView);
        ringv2 = (RingView) findComponentById(ResourceTable.Id_ring_view2);
        Button button = (Button) findComponentById(ResourceTable.Id_button);
        initRingView();
        button.setClickedListener(component -> {
            if (ringv2 != null && !mLabels.isEmpty())
                ringv2.setCenterTxt("迪迦");
            showRingView2(true);
        });
    }

    private void initRingView() {
        showRingView2(false);
    }

    private void showRingView2(boolean isNew) {
        int valueSum = 60;
        ArrayList<CircleBean> mList = new ArrayList<CircleBean>();
        mLabels.clear();
        CircleBean cb;
        int rvMax = ringv2.getMax();
        float startPro = 0f;
        int size = 3;
        for (int i = 0; i < size; i++) { // 计算数值占圆环的比例
            cb = new CircleBean();
            cb.setStartPro(startPro);

            switch (i) {
                case 0:
                    cb.setEndPro((12f / valueSum) * rvMax);
                    break;
                case 1:
                    cb.setEndPro((32f / valueSum) * rvMax);
                    break;
                default:
                    cb.setEndPro((16f / valueSum) * rvMax);
            }

            cb.setCenterPro(startPro + cb.getEndPro() / 2);
            if (isNew)
                cb.setDesc("di" + i + "ge");
            else
                cb.setDesc("ddv" + i + "n");
            mLabels.add(cb.getDesc());
            startPro += cb.getEndPro();
//            Common.log_d("201811091554", cb.toString())
            mList.add(cb);
        }
        ringv2.setData(mList, 1, 1);
        ringv2.setOuterIsEnable(true);
        ringv2.setListener(label -> {
            ringv2.setCenterTxt("艾欧尼亚");
            showRingView2(false);
        });

        setLegendData();
    }

    private void setLegendData() {
        mLegendView.setData(mLabels, ringv2.getColors());
    }
}
